﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace CIT.WMS.Other
{
    public partial class FrmFordBarCodeCheck : Form
    {
        public FrmFordBarCodeCheck()
        {
            InitializeComponent();
        }

        private void clearBoxLbl()
        {
            areaMsg.Text = "条码格式不正确，请确认！";
            txtBoxLablel.Text = "";
            txtBoxLablel.Focus();
        }
        private void clearPalletLbl()
        {
            areaMsg.Text = "条码格式不正确，请确认！";
            this.txtPalletLablel.Text = "";
            this.txtPalletLablel.Focus();
        }
        private string CheckString = "";
        private string PalletString = "";
        private void txtBoxLablel_KeyPress(object sender, KeyPressEventArgs e)
        {
            areaInfo.Text = "";
            if (e.KeyChar != 13)
            {
                return;
            }

            try
            {
                CheckString = "";
                //var snArray = txtBoxLablel.Text.Split(',');
                //if (snArray.Length != 11)
                //{
                //    splitBoxLabel(txtBoxLablel.Text);
                //    return;
                //}

                //var s7DC = snArray[7];
                //var s8PN = snArray[8];
                //var s9LOC1 = snArray[9];
                //var s9LOC2 = snArray[10];
                //CheckString = s7DC + s8PN + s9LOC1 + s9LOC2;
                CheckString = txtBoxLablel.Text;
                this.txtPalletLablel.Focus();
            }
            catch
            {
                clearBoxLbl();
                return;
            }
        }

        private bool splitBoxLabel(string str)
        {
            try
            {
                var idx1 = str.IndexOf("Q");
                CheckString = str.Substring(0, idx1);
                var nextStr = str.Substring(idx1);

                //--S
                var idx2 = nextStr.IndexOf("8V");
                nextStr = nextStr.Substring(idx2);

                var idx3 = nextStr.IndexOf("B");
                CheckString += nextStr.Substring(0, idx3);
                //--E

                //--S
                var idx4 = nextStr.IndexOf("1L");
                nextStr = nextStr.Substring(idx4);

                var idx5 = nextStr.IndexOf("3S");
                CheckString += nextStr.Substring(0, idx5);
                //--E

                //--S
                var idx6 = nextStr.IndexOf("20L");
                nextStr = nextStr.Substring(idx6);

                CheckString += nextStr;
                //--E
                this.txtPalletLablel.Focus();
                return true;
            }
            catch
            {
                clearBoxLbl();
                return false;
            }
        }

        private bool splitPalletLabel(string str)
        {
            try
            {
                var idx1 = str.IndexOf("Q");
                PalletString = str.Substring(0, idx1);
                var nextStr = str.Substring(idx1);

                //--S
                var idx2 = nextStr.IndexOf("8V");
                nextStr = nextStr.Substring(idx2);

                var idx3 = nextStr.IndexOf("B");
                PalletString += nextStr.Substring(0, idx3);
                //--E

                //--S
                var idx4 = nextStr.IndexOf("1L");
                nextStr = nextStr.Substring(idx4);

                var idx5 = nextStr.IndexOf("M");
                PalletString += nextStr.Substring(0, idx5);
                //--E

                //--S
                var idx6 = nextStr.IndexOf("20L");
                nextStr = nextStr.Substring(idx6);

                PalletString += nextStr;
                //--E

                if (CheckString.Equals(PalletString))
                {
                    areaInfo.Text = "Validation \r\nOK ";
                }
                else
                {
                    areaInfo.Text = "Validation \r\nNG";
                    MessageBox.Show("NG");
                }
                clearBoxLbl();
                clearPalletLbl();
                areaMsg.Text = "";

                return true;
            }
            catch
            {
                clearPalletLbl();
                return false;
            }
        }

        private void txtPalletLablel_KeyPress(object sender, KeyPressEventArgs e)
        {
            areaInfo.Text = "";
            if (e.KeyChar != 13)
            {
                return;
            }

            try
            {
                //var snArray = txtPalletLablel.Text.Split(',');
                //if (snArray.Length != 11)
                //{
                //    splitPalletLabel(txtPalletLablel.Text);
                //    return;
                //}

                //var s7DC = snArray[7];
                //var s8PN = snArray[8];
                //var s9LOC1 = snArray[9];
                //var s9LOC2 = snArray[10];
                //var palletString = s7DC + s8PN + s9LOC1 + s9LOC2;
                var palletString = txtPalletLablel.Text;

                if (CheckString.Equals(palletString))
                {
                    areaInfo.Text = "Validation \r\nOK ";
                }
                else
                {
                    areaInfo.Text = "Validation \r\nNG";
                    MessageBox.Show("NG");
                }
                clearBoxLbl();
                clearPalletLbl();
                areaMsg.Text = "";
            }
            catch
            {
                clearPalletLbl();
                return;
            }
        }

        //END
    }
}